//
//  LocalData.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "LocalData.h"

@implementation LocalData

+ (YTKKeyValueStore *)fetchValueStore {
    return [self fetchValueStore:[self fruitTableName]];
}
+ (YTKKeyValueStore *)fetchValueStore:(NSString *)table {
    
    YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"fruit.db"];
    [store createTableWithName:table];
    return store;
}
+ (NSString *)fruitTableName {
    return @"fruit_table";
}
+ (NSString *)fruitKey {
    return @"fruit";
}
+ (NSString *)fruitAddKey {
    return @"fruit_add";
}
+ (NSString *)fruitFinishKey {
    return @"fruit_finish";
}
+ (NSString *)dailyReminderTableName {
    return @"dailyReminder_table";
}
+ (NSString *)dailyReminderKey {
    return @"dailyReminder";
}

#pragma mark - 存储水果
///存储一个水果
+ (void)saveHomeFruitInfoModel:(FruitModel *)infoModel
{
    [self saveFruitInfoModel:infoModel fruitKey:[self fruitKey]];
}
///取出一个水果
+ (FruitModel *)getHomeFruit:(NSString *)vege_id
{
    return [self getFruit:vege_id fruitKey:[self fruitKey]];
}
///取出所有水果
+ (NSArray<FruitModel *> *)getAllHomeFruits
{
    return [self getAllFruits_fruitKey:[self fruitKey]];
}
///删除一个水果
+ (void)deleteHomeFruit:(NSString *)vege_id
{
    [self deleteFruit:vege_id fruitKey:[self fruitKey]];
}
///删除所有水果
+ (void)deleteAllHomeFruits
{
    [self deleteAllFruits_fruitKey:[self fruitKey]];
}
///取出水果数量
+ (NSInteger)allHomeFruitsCount
{
    return [self allFruitsCount_fruitKey:[self fruitKey]];
}

#pragma mark - 添加水果
///添加一个水果到添加列表
+ (void)saveAddFruitInfoModel:(FruitModel *)infoModel
{
    [self saveFruitInfoModel:infoModel fruitKey:[self fruitAddKey]];
}
///从添加列表取出一个水果
+ (FruitModel *)getAddFruit:(NSString *)vege_id
{
    return [self getFruit:vege_id fruitKey:[self fruitAddKey]];
}
///从添加列表取出所有水果
+ (NSArray<FruitModel *> *)getAllAddFruits
{
    return [self getAllFruits_fruitKey:[self fruitAddKey]];
}
///从添加列表删除一个水果
+ (void)deleteAddFruit:(NSString *)vege_id
{
    [self deleteFruit:vege_id fruitKey:[self fruitAddKey]];
}
///从添加列表删除所有水果
+ (void)deleteAllAddFruits
{
    [self deleteAllFruits_fruitKey:[self fruitAddKey]];
}
///从添加列表查询水果数量
+ (NSInteger)allAddFruitsCount
{
    return [self allFruitsCount_fruitKey:[self fruitAddKey]];
}

#pragma mark - 完成水果
///添加一个水果到完成列表
+ (void)saveFinishFruitInfoModel:(FruitModel *)infoModel
{
    [self saveFruitInfoModel:infoModel fruitKey:[self fruitFinishKey]];
}
///从完成列表取出一个水果
+ (FruitModel *)getFinishFruit:(NSString *)vege_id
{
    return [self getFruit:vege_id fruitKey:[self fruitFinishKey]];
}
///从完成列表取出所有水果
+ (NSArray<FruitModel *> *)getAllFinishFruits
{
    return [self getAllFruits_fruitKey:[self fruitFinishKey]];
}
///从完成列表删除一个水果
+ (void)deleteFinishFruit:(NSString *)vege_id
{
    [self deleteFruit:vege_id fruitKey:[self fruitFinishKey]];
}
///从完成列表删除所有水果
+ (void)deleteFinishAddFruits
{
    [self deleteAllFruits_fruitKey:[self fruitFinishKey]];
}
///从完成列表查询水果数量
+ (NSInteger)allFinishFruitsCount
{
    return [self allFruitsCount_fruitKey:[self fruitFinishKey]];
}


#pragma mark -

///存储一个水果
+ (void)saveFruitInfoModel:(FruitModel *)infoModel fruitKey:(NSString *)fruitKey
{
    NSDictionary *infoDic = infoModel.mj_keyValues;
    NSArray *dicFruits = [self getAlldicFruits_fruitKey:fruitKey];
    NSString *vege_id = nil;
    NSInteger index = 0;
    for (NSDictionary *dict in dicFruits) {
        NSString *dic_id = dict[@"vege_id"];
        if (infoModel.vege_id.integerValue == dic_id.integerValue) {
            vege_id = dic_id;
            break;
        }
        index++;
    }
    NSMutableArray *temDicFruits = [NSMutableArray arrayWithArray:dicFruits];
    if (vege_id) {
        [temDicFruits replaceObjectAtIndex:index withObject:infoDic];
    } else {
        [temDicFruits addObject:infoDic];
    }
    [[self fetchValueStore] putObject:temDicFruits withId:fruitKey intoTable:[self fruitTableName]];
}
///取出一个水果
+ (FruitModel *)getFruit:(NSString *)vege_id fruitKey:(NSString *)fruitKey
{
    NSArray *dicVfruits = [self getAlldicFruits_fruitKey:fruitKey];
    FruitModel *infoModel = nil;
    for (NSDictionary *dict in dicVfruits) {
        NSString *dic_id = dict[@"vege_id"];
        if (vege_id.integerValue == dic_id.integerValue) {
            infoModel = [FruitModel mj_objectWithKeyValues:dict];
            break;
        }
    }
    return infoModel;
}
///取出所有水果
+ (NSArray<FruitModel *> *)getAllFruits_fruitKey:(NSString *)fruitKey
{
    NSArray *dicVfruits = [self getAlldicFruits_fruitKey:fruitKey];
    NSMutableArray *temModelArr = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in dicVfruits) {
        FruitModel *model = [FruitModel mj_objectWithKeyValues:dict];
        [temModelArr addObject:model];
    }
    return temModelArr;
}
+ (NSArray *)getAlldicFruits_fruitKey:(NSString *)fruitKey
{
    YTKKeyValueItem *item = [[self fetchValueStore] getYTKKeyValueItemById:fruitKey fromTable:[self fruitTableName]];
    NSArray *result = (NSArray *)item.itemObject;
    if (result.count == 0) {
        return nil;
    }
    NSArray *dicFruits = [[self fetchValueStore] getObjectById:fruitKey fromTable:[self fruitTableName]];
    return dicFruits;
}
///删除一个水果
+ (void)deleteFruit:(NSString *)vege_id fruitKey:(NSString *)fruitKey
{
    NSArray *dicFruits = [self getAlldicFruits_fruitKey:fruitKey];
    NSMutableArray *temDicFruits = [NSMutableArray arrayWithArray:dicFruits];
    NSDictionary *infoDict = nil;
    for (NSDictionary *dict in dicFruits) {
        NSString *dic_id = dict[@"vege_id"];
        if (vege_id.integerValue == dic_id.integerValue) {
            infoDict = dict;
            break;
        }
    }
    if (infoDict) {
        [temDicFruits removeObject:infoDict];
    }
    if (temDicFruits.count == 0) {
        [self deleteAllFruits_fruitKey:fruitKey];
    } else {
        [[self fetchValueStore] putObject:temDicFruits withId:fruitKey intoTable:[self fruitTableName]];
    }
}
///删除所有水果
+ (void)deleteAllFruits_fruitKey:(NSString *)fruitKey
{
    [[self fetchValueStore] deleteObjectById:fruitKey fromTable:[self fruitTableName]];
}

+ (NSInteger)allFruitsCount_fruitKey:(NSString *)fruitKey
{
    NSInteger count = 0;
    NSArray *dicVfruits = [self getAlldicFruits_fruitKey:fruitKey];
    if (dicVfruits.count) {
        count = dicVfruits.count;
    }
    return count;
}

+ (void)saveFirstData
{
    NSArray *fruits = [self getAlldicFruits_fruitKey:[self fruitKey]];
    if (fruits.count == 0) {
        NSArray *modelTitles = @[@"香蕉", @"苹果", @"橙子", @"葡萄", @"甜瓜", @"西瓜", @"车厘子", @"榴莲", @"龙眼", @"猕猴桃"];
        NSArray *modelImageStrs = @[@"xiangjiao", @"pingguo", @"chengzi", @"putao", @"tiangua", @"xigua", @"chelizi", @"liulian", @"longyan", @"mihoutao"];
        for (NSInteger i = 0; i < modelTitles.count; i++) {
            FruitModel *model = [[FruitModel alloc] init];
            model.vege_id = [NSString stringWithFormat:@"%ld", i];
            model.title = modelTitles[i];
            model.imageStr = modelImageStrs[i];
            [self saveHomeFruitInfoModel:model];
        }
    }
}

///保存每日提醒时间
+ (void)saveDailyReminder:(NSString *)timeStr
{
    [[self fetchValueStore:[self dailyReminderTableName]] putString:timeStr withId:[self dailyReminderKey] intoTable:[self dailyReminderTableName]];
}
///得到每日提醒时间
+ (NSString *)getDailyReminder
{
    NSString *string = nil;
    YTKKeyValueItem *item = [[self fetchValueStore:[self dailyReminderTableName]] getYTKKeyValueItemById:[self dailyReminderKey] fromTable:[self dailyReminderTableName]];
    NSArray *result = (NSArray *)item.itemObject;
    if (result.count == 0) {
        return nil;
    }
    string = [[self fetchValueStore:[self dailyReminderTableName]] getStringById:[self dailyReminderKey] fromTable:[self dailyReminderTableName]];
    return string;
}
///删除每日提醒时间
+ (void)deleteDailyReminder
{
    [[self fetchValueStore:[self dailyReminderTableName]] deleteObjectById:[self dailyReminderKey] fromTable:[self dailyReminderTableName]];
}
    
@end
